require "Common/define"
require "Logic/Config"
require "Net/send_Msg"
require "proto/dungeon/message/ReqDungeonRandomEventAcceptMessage"
require "proto/dungeon/message/ReqDungeonRandomEventShopBuyMessage"
require "proto/dungeon/message/ReqDungeonRandomEventBattleMessage"
require "proto/dungeon/message/ReqDungeonRandomEventFoeInfoMessage"
require "proto/dungeon/message/ReqDungeonRandomEventQuestionMessage"

DungeonRandomEventHandler = DungeonRandomEventHandler or {};

local buyShop;
local buyNum;

function DungeonRandomEventHandler.getDungeonRandomEventAccept(msg)
    if msg.state == 0 then
        logicMgr.DungeonRandomEventManager.setDungeonRandomEventData(msg.dungeonRandomEventTimes, {type = 0, overdueTime = 0});
        logicMgr.DungeonRandomEventManager.setDungeonRandomEventHeroData(msg.bean);
        FireEvent(Config.EventType.Fresh_DungeonRandomEventAccept, true);
    end
end

function DungeonRandomEventHandler.getDungeonRandomEventShopBuy(msg)
    if msg.state == 0 then
        local bean = msg.bean;
        if bean == nil or bean.type == nil then
            logicMgr.DungeonRandomEventManager.setDungeonRandomEventData(msg.dungeonRandomEventTimes, {type = 0, overdueTime = 0});
        else
            logicMgr.DungeonRandomEventManager.setDungeonRandomEventData(nil, msg.bean);
        end
        DungeonRandomEventHandler.TADungeonRandomEventShopBuy();
        --logicMgr.DungeonRandomEventManager.setDungeonRandomEventMaxCount();
        --logicMgr.DungeonRandomEventManager.getFetchReward(msg.items);
        FireEvent(Config.EventType.Fresh_DungeonRandomEventAccept, msg.items);
    end
end

function DungeonRandomEventHandler.TADungeonRandomEventShopBuy()
    if buyShop ~= nil then
        local _shopDic = {}
        _shopDic.shop_id = tostring(EnumConst.ShopType.rankShop)                 --EnumConst.ShopType 枚举值
        _shopDic.shop_buy_count = tonumber(buyNum)                               --商店购买物品的数量
        _shopDic.shop_buy_price = tonumber(buyShop["price"])                     --商店购买的单价，t_shopgoods.f_Price
        _shopDic.shop_buy_cost = tonumber(buyNum) *tonumber(buyShop["price"])    --商店购买的总花费
        _shopDic.shop_money_type = tostring(buyShop["monyType"])                 --使用的货币类型
        _shopDic.shop_item_id = tostring(buyShop["itemModelId"])                 -- t_item 表id
        _shopDic.shop_commodity_id = tostring(buyShop["eventShopCommodityId"])   -- t_shopgoods 表id

        TAManager.TrackByDictionary("shop_buy",_shopDic);
        buyShop = nil;
    end
end

function DungeonRandomEventHandler.getDungeonRandomEventBattle(msg)
    if msg.state == 0 then
        logicMgr.DungeonRandomEventManager.setDungeonRandomEventFightResult(msg);
        local enemyData = logicMgr.DungeonRandomEventManager.getDungeonRandomEventData();
        if enemyData ~= nil and enemyData["foePlayerId"] ~= nil then
            local randomEvent = {}
            randomEvent.player_id = enemyData["foePlayerId"].playerId;
            TAManager.TrackByDictionary("dungeonRandomEventPK",randomEvent);
        end
        logicMgr.DungeonRandomEventManager.setDungeonRandomEventData(nil, {type = 0, overdueTime = 0});
        FireEvent(Config.EventType.Fresh_DungeonRandomEventAccept);
        logicMgr.BattlePlaybackManager.RequestReport(msg.fightId, msg.fightResult, msg.record);
        logicMgr.BattlePlaybackManager.ResetReceiveReport();
        logicMgr.BattlePlaybackManager.GotoFight(EnumConst.FightTypeConst.DungeonEventPK);
    end
end

function DungeonRandomEventHandler.getDungeonRandomEventFoeInfo(msg)
    if msg.state == 0 then
        FireEvent(Config.EventType.Fresh_DungeonRandomEventAccept, msg);
    end
end

function DungeonRandomEventHandler.getDungeonRandomEventQuestion(msg)
    if msg.state == 0 then
        logicMgr.DungeonRandomEventManager.setDungeonRandomEventData(nil, {type = 0, overdueTime = 0});
        FireEvent(Config.EventType.Fresh_DungeonRandomEventAccept, msg);
    end
end

function DungeonRandomEventHandler.sendDungeonRandomEventAccept()
    local req = ReqDungeonRandomEventAcceptMessage.New();
    req.accept = 1;
    sendReqMessage(req);
end

function DungeonRandomEventHandler.sendDungeonRandomEventShopBuy(item, num)
    local req = ReqDungeonRandomEventShopBuyMessage.New();
    buyShop = item;
    buyNum = num;
    req.index = item["shopItemId"];
    sendReqMessage(req);
end

function DungeonRandomEventHandler.sendDungeonRandomEventBattle(formation)
    local req = ReqDungeonRandomEventBattleMessage.New();
    req.formation = formation;
    sendReqMessage(req);
end

function DungeonRandomEventHandler.sendDungeonRandomEventFoeInfo()
    local req = ReqDungeonRandomEventFoeInfoMessage.New();
    sendReqMessage(req);
end

function DungeonRandomEventHandler.sendDungeonRandomEventQuestion(index)
    local req = ReqDungeonRandomEventQuestionMessage.New();
    req.answer = index;
    sendReqMessage(req);
end
